package com.whu.waterworks.util;

import org.apache.commons.beanutils.PropertyUtils;
import org.java_websocket.client.WebSocketClient;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.alibaba.fastjson.JSON;
import com.whu.waterworks.dto.AlertRecordTimerDto;
import com.whu.waterworks.mina.MyWebSocketClient;
import com.whu.waterworks.model.AlertRecord;
import com.whu.waterworks.service.AlertRecordService;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author cai 每一分钟推送未处理报警信息
 */
public class SpringQtz {
	private static int counter = 0;
	private AlertRecordService alertRecordService;
	private List<AlertRecord> alertRecords = new ArrayList<>();
	private String alertMapString;

	protected void execute() {

		/*
		 * long ms = System.currentTimeMillis(); System.out.println("\t\t" + new
		 * Date(ms)); System.out.println("(" + counter++ + ")");
		 */
		String startTime="", endTime="";
		
		startTime = "0";
		SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy/MM/dd/hh:mm:ss");
		Date currentTimeDam = new java.util.Date();// 得到当前系统时间
		endTime = formatter.format(currentTimeDam);

		alertRecords = alertRecordService.getUndisposeRecords(startTime,
				endTime);

		// 如果还有未处理的预警信息,开始推送
		if (alertRecords != null && alertRecords.size() != 0) {
			try {
				

				/*
				}*/
				
				for (AlertRecord alertRecord : alertRecords) {
					AlertRecordTimerDto alertRecordTimerDto =new AlertRecordTimerDto();
					PropertyUtils.copyProperties(alertRecordTimerDto, alertRecord);
					alertMapString = JSON.toJSONString(alertRecordTimerDto);
					System.out.println("alertMapString:" + alertMapString);
					 //String s="你好";
					WebSocketClient cc = MyWebSocketUtil.getInstance();
					cc.send(alertMapString);
					
				}
				
			/*	//cc.getConnection().close(1000);
				
				cc.getConnection().close();
				//cc.onClose(1000, "close", true);
				cc.close();*/
				
				
				

			} catch (Exception e) {
				 System.out.println(e.toString());
				e.printStackTrace();

			}
		}

	}

	public static int getCounter() {
		return counter;
	}

	public static void setCounter(int counter) {
		SpringQtz.counter = counter;
	}

	public AlertRecordService getAlertRecordService() {
		return alertRecordService;
	}

	public void setAlertRecordService(AlertRecordService alertRecordService) {
		this.alertRecordService = alertRecordService;
	}

	public List<AlertRecord> getAlertRecords() {
		return alertRecords;
	}

	public void setAlertRecords(List<AlertRecord> alertRecords) {
		this.alertRecords = alertRecords;
	}

	public String getAlertMapString() {
		return alertMapString;
	}

	public void setAlertMapString(String alertMapString) {
		this.alertMapString = alertMapString;
	}
}